package com.softsec.isc.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class FilterResult {
	
	static Logger logger = LogManager.getLogger("FilterResult");
	
	private final static FilterResult fr = new FilterResult();
	
	private Map<String, ArrayList<String>> packageMap = new HashMap<String, ArrayList<String>>(); 
	private Map<String, ArrayList<String>> stringMap = new HashMap<String, ArrayList<String>>(); 
	private Map<String, ArrayList<String>> appnameMap = new HashMap<String, ArrayList<String>>(); 
	
	public static FilterResult getInstant(){
		return fr;
	}
	
	public void  addToPackageMap(String str, String apkName) {
		if (packageMap.get(str) == null) {
			ArrayList<String> appList = new ArrayList<String>(); 
			appList.add(apkName);
			packageMap.put(str, appList);
		}
		else {
			packageMap.get(str).add(apkName);
		}
	}
	public void  addToStringMap(String str, String apkName) {
		if (stringMap.get(str) == null) {
			ArrayList<String> appList = new ArrayList<String>(); 
			appList.add(apkName);
			stringMap.put(str, appList);
		}
		else {
			stringMap.get(str).add(apkName);
		}
	}
	public void  addToAppMap(String str, String apkName) {
		if (appnameMap.get(str) == null) {
			ArrayList<String> appList = new ArrayList<String>(); 
			appList.add(apkName);
			appnameMap.put(str, appList);
		}
		else {
			appnameMap.get(str).add(apkName);
		}
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void printResult(){
		logger.warn("\n\n");
		//Print application result...
		Iterator appNameIterator = appnameMap.entrySet().iterator();
		while (appNameIterator.hasNext()) {
			Map.Entry entry = (Map.Entry) appNameIterator.next();
			String key = (String) entry.getKey();
			ArrayList<String> valueList = (ArrayList<String>) entry.getValue();
			logger.warn("匹配字符串[" + key + "]:");
			for (String value: valueList) {
				logger.warn("\t- " + value);
			}
		}
		//print package result...
		logger.warn("\n");
		Iterator packageIterator = packageMap.entrySet().iterator();
		while (packageIterator.hasNext()) {
			Map.Entry entry = (Map.Entry) packageIterator.next();
			String key = (String) entry.getKey();
			ArrayList<String> valueList = (ArrayList<String>) entry.getValue();
			logger.warn("匹配包名[" + key + "]:");
			for (String value: valueList) {
				logger.warn("\t- " + value);
			}
		}
		//print string result...
		logger.warn("\n");
		Iterator stringIterator = stringMap.entrySet().iterator();
		while (stringIterator.hasNext()) {
			Map.Entry entry = (Map.Entry) stringIterator.next();
			String key = (String) entry.getKey();
			ArrayList<String> valueList = (ArrayList<String>) entry.getValue();
			logger.warn("匹配字符串[" + key + "]:");
			for (String value: valueList) {
				logger.warn("\t- " + value);
			}
		}
	}
}
